﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRPM_IN301010UV01;
using YLW.Plat.Jhfw.Model.JhfwRygl;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRPM_IN301010UV01
    {
        public static string ProviderInfoRegister(AddJhfwRyglIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //性别
            string sexCode = "";
            string sexName = "";
            switch (jsonData.sex)
            {
                case "B":
                    sexCode = "9";
                    sexName = "未说明的性别";
                    break;
                case "F":
                    sexCode = "2";
                    sexName = "女性";
                    break;
                case "M":
                    sexCode = "1";
                    sexName = "男性";
                    break;
                case "U":
                    sexCode = "0";
                    sexName = "未知的性别";
                    break;
            }

            //创建根对象
            var xmlRoot = new PRPM_IN301010UV01
            {
                ITSVersion = "XML_1.0",
                id1 = new id1 { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRPM_IN301010UV01" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = receiver_name
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id1 = new id1
                        {
                            item = new item
                            {
                                root = "2.16.156.10011.2.5.1.3",
                                extension = sender_name
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        registrationRequest = new registrationRequest
                        {
                            classCode = "REG",
                            moodCode = "RQO",
                            statusCode = new statusCode { code = "active" },
                            subject1 = new subject1
                            {
                                typeCode = "SBJ",
                                healthCareProvider = new healthCareProvider
                                {
                                    classCode = "PROV",
                                    id1 = new id1
                                    {
                                        item = new item { root = "2.16.156.10011.1.4", extension = jsonData.empid }
                                    },
                                    code = new code
                                    {
                                        code1 = "",
                                        codeSystem = "2.16.156.10011.2.3.3.10",
                                        codeSystemName = "专业技术职务代码（ GB/T 8561）",
                                        displayName = new displayName { value = "" }
                                    },
                                    addr = new addr(),
                                    telecom = new telecom(),
                                    statusCode = new statusCode { code = "active" },
                                    effectiveTime = new effectiveTime
                                    {
                                        low = new low { value = jsonData.rzsj },
                                        high = new high { value = jsonData.lzsj },
                                    },
                                    healthCarePrincipalPerson = new healthCarePrincipalPerson
                                    {
                                        classCode = "",
                                        determinerCode = "",

                                        id1 = new id1
                                        {
                                            item = new item
                                            {
                                                root = "2.16.156.10011.1.3",
                                                extension = jsonData.pcid
                                            }
                                        },
                                        idCategory = new idCategory
                                        {
                                            code = "01",
                                            codeSystem = "2.16.156.10011.2.3.1.1",
                                            codeSystemName = "身份证件类别代码表",
                                            displayName = new displayName { value = "居民身份证" }
                                        },
                                        name = new name
                                        {
                                            item = new item
                                            {
                                                part = new part { value = jsonData.empname }
                                            }
                                        },
                                        administrativeGenderCode = new administrativeGenderCode
                                        {
                                            code = sexCode,
                                            codeSystem = "2.16.156.10011.2.3.3.4",
                                            codeSystemName = "生理性别代码表(GB/T 2261.1)",
                                            displayName = new displayName
                                            {
                                                value = sexName
                                            }
                                        },
                                        birthTime = new birthTime { value = jsonData.birthday },
                                        asAffiliate = new asAffiliate
                                        {
                                            classCode = "AFFL",
                                            code = new code(),
                                            effectiveTime = new effectiveTime(),
                                            affiliatedPrincipalOrganization = new affiliatedPrincipalOrganization
                                            {
                                                classCode = "ORG",
                                                determinerCode = "INSTANCE",
                                                id1 = new id1
                                                {
                                                    item = new item
                                                    {
                                                        root = "2.16.156.10011.1.26",
                                                        extension = jsonData.deptid
                                                    }
                                                },
                                                name = new name
                                                {
                                                    item = new item
                                                    {
                                                        part = new part { value = "" }
                                                    }
                                                }
                                            }
                                        },
                                        birthplace = new birthplace
                                        {
                                            classCode = "BIRTHPL",
                                            addr = new addr()
                                        }
                                    }
                                }
                            },
                            author = new author
                            {
                                typeCode = "AUT",
                                assignedEntity = new assignedEntity
                                {
                                    classCode = "ASSIGNED",
                                    id1 = new id1
                                    {
                                        item = new item
                                        {
                                            root = "2.16.156.10011.1.4",
                                            extension = jsonData.in_empid
                                        }
                                    },
                                    assignedPerson = new assignedPerson
                                    {
                                        name = new name
                                        {
                                            item = new item
                                            {

                                            }
                                        }
                                    }
                                }
                            },
                            extension = new extension
                            {
                                dotype = new value { value1 = jsonData.dotype },
                                sjly = new value { value1 = jsonData.sjly },
                                lyxt = new value { value1 = jsonData.lyxt },
                                hoscode = new value { value1 = jsonData.hoscode },
                                empid = new value { value1 = jsonData.empid },
                                empname = new value { value1 = jsonData.empname },
                                oaid = new value { value1 = jsonData.oaid },
                                sex = new value { value1 = jsonData.sex },
                                zjlx = new value { value1 = jsonData.zjlx },
                                pcid = new value { value1 = jsonData.pcid },
                                birthday = new value { value1 = jsonData.birthday },
                                birthaddress = new value { value1 = jsonData.birthaddress },
                                cellphone = new value { value1 = jsonData.cellphone },
                                netphone = new value { value1 = jsonData.netphone },
                                py = new value { value1 = jsonData.py },
                                title = new value { value1 = jsonData.title },
                                ismedgroup = new value { value1 = jsonData.ismedgroup },
                                medgroup = new value { value1 = jsonData.medgroup },
                                empintroduction = new value { value1 = jsonData.empintroduction },
                                deptid = new value { value1 = jsonData.deptid },
                                rzsj = new value { value1 = jsonData.rzsj },
                                lzsj = new value { value1 = jsonData.lzsj },
                                remark = new value { value1 = jsonData.remark },
                                ybysdm = new value { value1 = jsonData.ybysdm },
                                ybhsdm = new value { value1 = jsonData.ybhsdm },
                                certificateno = new value { value1 = jsonData.certificateno },
                                technicianno = new value { value1 = jsonData.technicianno },
                                zydd = new value { value1 = jsonData.zydd },
                                useflag = new value { value1 = jsonData.useflag },
                                in_empid = new value { value1 = jsonData.in_empid },
                                in_date = new value { value1 = jsonData.in_date },
                                xg_empid = new value { value1 = jsonData.xg_empid },
                                xgsj = new value { value1 = jsonData.xgsj }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
